package org.budo.canal.parse.inbound.mysql;

import org.springframework.beans.factory.FactoryBean;

import com.alibaba.otter.canal.parse.CanalEventParser;
import com.alibaba.otter.canal.parse.ha.CanalHAController;
import com.alibaba.otter.canal.parse.ha.HeartBeatHAController;
import com.alibaba.otter.canal.parse.inbound.mysql.rds.RdsBinlogEventParserProxy;
import com.alibaba.otter.canal.parse.index.CanalLogPositionManager;
import com.alibaba.otter.canal.parse.index.MemoryLogPositionManager;
import com.alibaba.otter.canal.parse.support.AuthenticationInfo;
import com.alibaba.otter.canal.sink.CanalEventSink;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * @author limingwei
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
public class MysqlEventParserFactoryBean implements FactoryBean<CanalEventParser> {
    private String destination = "MysqlEventParserFactoryBean.destination"; // MetaLogPositionManager 部分会用到

    private CanalLogPositionManager logPositionManager = new MemoryLogPositionManager();

    private CanalHAController haController = new HeartBeatHAController();

    private CanalEventSink eventSink;

    private AuthenticationInfo masterInfo;

    private Boolean filterTableError = false;

    @Override
    public CanalEventParser getObject() throws Exception {
        return this.build();
    }

    public CanalEventParser build() {
        RdsBinlogEventParserProxy eventParser = new RdsBinlogEventParserProxy();
        eventParser.setDestination(this.getDestination());
        eventParser.setEventSink(this.getEventSink());
        eventParser.setLogPositionManager(this.getLogPositionManager());
        eventParser.setHaController(this.getHaController());
        eventParser.setMasterInfo(this.getMasterInfo());
        eventParser.setFilterTableError(this.getFilterTableError());
        return eventParser;
    }

    @Override
    public Class<?> getObjectType() {
        return CanalEventParser.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
